
import { getByTypes } from "_p/basic/api/admin/datadic";
import {multiply} from "_p/afs-apply/assets/js/utils";

export function getDictDataList(arr) {
    return new Promise((resolve, reject) => {
        var data = {},
            queryArr = new Set();
        Array.isArray(arr) && arr.forEach(ele => {
            queryArr.add(ele)
        })
        if (queryArr.size > 0) {
            getByTypes([...queryArr]).then(res => {
                if (res.code === "0000" && res.data) {
                    Object.keys(res.data).forEach(ele => {
                        data[ele + 'Dict'] = res.data[ele];
                    })
                } else {
                    console.error('获取数据字典出错:', [...queryArr])
                }
                resolve(data);
            });
        } else {
            resolve(data);
        }
    });
}

// 两数相加
export function add(arg1, arg2) {
    var r1, r2, m;
    try {
        r1 = arg1.toString().split(".")[1].length;
    } catch (e) {
        r1 = 0;
    }
    try {
        r2 = arg2.toString().split(".")[1].length;
    } catch (e) {
        r2 = 0;
    }
    m = Math.pow(10, Math.max(r1, r2));
    return (multiply(arg1, m) + multiply(arg2, m)) / m;
}
// 数据字典 value转title
export function getTitleFromDataDict(data, val) {
    if (!val || !Array.isArray(data)) return '';
    let d = data.filter(i => i.value == val);
    if (d[0] && d[0].title) {
        return d[0].title;
    }
    // return val; // 没有数据返回原值
    console.warn('数据字典值[' + val + ']转 title 有错误;',);
    return '';
}
